var module=angular.module('footer', []);

module.component('footer', {
    transclude: {
        'left':'?left',
        'right': '?right'
    },
    template: `
<div class="footer">
    <ng-transclude ng-transclude-slot="left">
        <input ng-show="!$ctrl.hideLeft" type="button" value="首 页" name="exit" class="exit" voice-control="首页,退出" onclick="javascript:PageForward.toMain();"/>
    </ng-transclude>
    <ng-transclude ng-transclude-slot="right">
    </ng-transclude>
    <div class="timing">剩余操作时间：<span ng-show="$ctrl.countDown">{{$ctrl.countDown}}</span></div>
</div>
    `,
    controller: function ($scope,footerService) {
        if(this.countDown==0){
            return
        }
        var self = this;
        self.$onInit = async function() {
            while (!self.countDown){//等待赋值完成
                await $sleep(10)
            }

            while (self.countDown>0){
                if(self.destroyed){
                    return
                }
                await $sleep(1000)
                self.countDown--
                $scope.$apply()
            }
            PageForward.toMain()
        }

        const hideLeftCallbackId=new Date().getTime()

        self.$onDestroy=function () {
            self.destroyed=true
            footerService.removeHideLeft(hideLeftCallbackId)
        }

        footerService.onNotifyHideLeft(`${hideLeftCallbackId}`,function () {
            self.hideLeft=true
        })
    },
    bindings:{
        countDown:'<'
    }
});

module.factory("footerService", function() {
    const hideLeftCallbacks:{ [key: string]: Function }  = {};
    const notifyHideLeft=function () {
        for(let funId in hideLeftCallbacks){
            hideLeftCallbacks[funId]()
        }
    }
    const onNotifyHideLeft=function (hideLeftCallbackId:string,callback:Function) {
        hideLeftCallbacks[hideLeftCallbackId]=callback
    }
    const removeHideLeft=function (hideLeftCallbackId:string) {
        delete hideLeftCallbacks[hideLeftCallbackId]
    }
    return {
        onNotifyHideLeft: onNotifyHideLeft,
        notifyHideLeft: notifyHideLeft,
        removeHideLeft:removeHideLeft
    }
});